﻿using Microsoft.VisualBasic;
using MiniExcelLibs;
using MiniExcelLibs.Attributes;
using Newtonsoft.Json;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using static System.Runtime.InteropServices.JavaScript.JSType;

namespace ConsoleApp10
{
    internal class Program
    {
        static void Main(string[] args)
        {
            string folderPath = @"C:\Users\humengjian\Desktop\新建文件夹\判断";

            // 获取指定文件夹下的所有文件
            string[] files = Directory.GetFiles(folderPath);

            // 遍历并输出每个文件的路径
            foreach (string file in files)
            {
                string fileName = Path.GetFileNameWithoutExtension(file);



                using (Stream stream = File.OpenRead(file))
                {
                    List<string> list = new List<string>();
                    var questionInputs = stream.Query<QuestionImportDataV2>().ToList();
                    if (questionInputs == null || questionInputs.Count == 0)
                    {
                        return;
                    }
                    //List<QuestionInput> questions = new List<QuestionInput>();
                    //foreach (var questionInput in questionInputs)
                    //{
                    //    //var strings = questionInput.Content.Split("\n");
                    //    //list.AddRange(strings);
                    //    string[] lines = questionInput.Content.Split('\n');
                    //    foreach (string line in lines)
                    //    {
                    //        Match match = Regex.Match(line, @"^(\d+) 、(.+)\((\√|X)\)$");
                    //        if (match.Success)
                    //        {
                    //            int number = int.Parse(match.Groups[1].Value);
                    //            string content = match.Groups[2].Value.Trim();
                    //            string result = match.Groups[3].Value;
                    //            Console.WriteLine($"{number} 、{content}({result})");

                    //            QuestionInput questionInput1 = new QuestionInput();
                    //            List<WxOptionsInfo> wxOptionsInfos = new List<WxOptionsInfo>();
                    //            wxOptionsInfos.Add(new WxOptionsInfo()
                    //            {
                    //                OptionTitle = "A",
                    //                OptionContent = "对"
                    //            });
                    //            wxOptionsInfos.Add(new WxOptionsInfo()
                    //            {
                    //                OptionTitle = "B",
                    //                OptionContent = "错"
                    //            });
                    //            questionInput1.Id = number;
                    //            questionInput1.Contents = content;
                    //            questionInput1.Answer = result == "√" ? "A" : "B";
                    //            questionInput1.Options = JsonConvert.SerializeObject(wxOptionsInfos);
                    //            questions.Add(questionInput1);
                    //        }
                    //        else
                    //        {
                    //            Console.WriteLine("匹配失败");
                    //        }
                    //    }
                    //}
                    //var sss = questions.GroupBy(a => a.Id);
                    //foreach (var ss in sss)
                    //{
                    //    Console.WriteLine(ss.Key + "  " + ss.Count());
                    //}

                    List<QuestionOutDataV3> questionOutDataV3s = new List<QuestionOutDataV3>();

                    foreach (var item in questionInputs)
                    {
                        QuestionOutDataV3 questionOutDataV3 = new QuestionOutDataV3()
                        {
                            Answer = item.Answer == "正确" ? "A" : "B",
                            ApplyArea = "全国",
                            OptionA = "正确",
                            OptionB = "错误",
                            Content = item.Content,
                            Number = item.Num,
                            QuestionType = "单选题"
                        };
                        questionOutDataV3s.Add(questionOutDataV3);
                    }
                    using (var strem = File.Create($"{fileName}out.xlsx"))
                    {
                        strem.SaveAs(questionOutDataV3s);
                    }


                    //List<QuestionImportDataV1> inputs= new List<QuestionImportDataV1>();
                    //foreach (var item in questionInputs)
                    //{
                    //    QuestionInput questionInput = new QuestionInput();
                    //    questionInput.WorkType = "危险货物装卸试题";
                    //    questionInput.Contents = item.Content;
                    //    List <WxOptionsInfo> wxOptions=new List<WxOptionsInfo>();
                    //    if (!string.IsNullOrEmpty(item.OptionA))
                    //    {
                    //        wxOptions.Add(new WxOptionsInfo()
                    //        {
                    //            OptionTitle = "A",
                    //            OptionContent = item.OptionA,
                    //        });

                    //    }
                    //    if (!string.IsNullOrEmpty(item.OptionB))
                    //    {
                    //        wxOptions.Add(new WxOptionsInfo()
                    //        {
                    //            OptionTitle = "B",
                    //            OptionContent = item.OptionB,
                    //        });

                    //    }
                    //    if (!string.IsNullOrEmpty(item.OptionC))
                    //    {
                    //        wxOptions.Add(new WxOptionsInfo()
                    //        {
                    //            OptionTitle = "C",
                    //            OptionContent = item.OptionC,
                    //        });
                    //    }
                    //    if (!string.IsNullOrEmpty(item.OptionD))
                    //    {
                    //        wxOptions.Add(new WxOptionsInfo()
                    //        {
                    //            OptionTitle = "D",
                    //            OptionContent = item.OptionD,
                    //        });
                    //    }
                    //    string answwer = "";
                    //    switch (item.Answer)
                    //    {
                    //        case "1":
                    //            answwer = "A";
                    //            break;
                    //        case "2":
                    //            answwer = "B";
                    //            break;
                    //        case "3":
                    //            answwer = "C";
                    //            break;
                    //        case "4":
                    //            answwer = "D";
                    //            break;
                    //        default:
                    //            break;
                    //    }
                    //    questionInput.Answer = answwer;
                    //    questionInput.Options = JsonConvert.SerializeObject(wxOptions);
                    //    inputs.Add(questionInput);
                    //}

                }
            }
        }
    }
    public class QuestionInput
    {
        #region 实体成员
        public long Id { get; set; }
        /// <summary>
        /// 准操项目
        /// </summary>	
        public string WorkType { get; set; }
        /// <summary>
        /// 题目类型 1判断题  2单选题  3多选题 
        /// </summary>		
        public int QuestionType { get; set; }
        /// <summary>
        /// 题干内容
        /// </summary>		
        public string Contents { get; set; }
        /// <summary>
        /// 问题答案
        /// </summary>		

        public string Answer { get; set; }
        /// <summary>
        /// 选项  JSON格式
        /// </summary>		
        public string Options { get; set; }
        /// <summary>
        /// 题目解析-知识点 
        /// </summary> 
        public string Explain { get; set; }

        /// <summary>
        /// 启用禁用  1：启用 0：禁用
        /// </summary>	
        public int EnabledMark { get; set; } = 1;

        public int IsInner { get; set; } = 0;

        #endregion
    }


    public class WxOptionsInfo
    {
        /// <summary>
        ///  A B C D
        /// </summary>
        public string OptionTitle { get; set; }
        /// <summary>
        /// 考试内容
        /// </summary>
        public string OptionContent { get; set; }

    }
    public class QuestionImportData
    {
        [ExcelColumnName("序号")]
        public int? Number { get; set; }
        [ExcelColumnName("题型")]
        public string QuestionType { get; set; } = "单选题";
        [ExcelColumnName("题目")]
        public string Content { get; set; }
        [ExcelColumnName("答案1")]
        public string OptionA { get; set; }
        [ExcelColumnName("答案2")]
        public string OptionB { get; set; }
        [ExcelColumnName("答案3")]
        public string OptionC { get; set; }
        [ExcelColumnName("答案4")]
        public string OptionD { get; set; }

        [ExcelColumnName("正确答案")]
        public string Answer { get; set; }
    }

    public class QuestionImportDataV1
    {
        [ExcelColumnName("单项选择题")]
        public string Content { get; set; }


    }
    public class QuestionImportDataV2
    {
        [ExcelColumnName("序号")]
        public int Num { get; set; }
        [ExcelColumnName("题干")]
        public string Content { get; set; }
        [ExcelColumnName("答案")]
        public string Answer { get; set; }
        [ExcelColumnName("区域")]
        public string Applera { get; set; }


    }

    public class QuestionOutDataV3

    {
        [ExcelColumnName("序号")]
        public int? Number { get; set; }
        [ExcelColumnName("题型")]
        public string QuestionType { get; set; } = "单选题";
        [ExcelColumnName("题目")]
        public string Content { get; set; }
        [ExcelColumnName("答案1")]
        public string OptionA { get; set; }
        [ExcelColumnName("答案2")]
        public string OptionB { get; set; }
        [ExcelColumnName("答案3")]
        public string OptionC { get; set; }
        [ExcelColumnName("答案4")]
        public string OptionD { get; set; }

        [ExcelColumnName("正确答案")]
        public string Answer { get; set; }

        [ExcelColumnName("备注")]
        public string Remark { get; set; }

        [ExcelColumnName("区域")]
        public string ApplyArea { get; set; } = "全国";
    }

}
